package net.wxxr.thread.pool;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.SocketTimeoutException;

import org.apache.http.conn.ConnectTimeoutException;

import com.ykdl.member.base.QLog;

import net.wxxr.dataparse.model.DataParseCode;
import net.wxxr.dataparse.model.HttpCode;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.model.pojo.HttpResult;
import net.wxxr.http.request.HttpBaseRequest;
import net.wxxr.http.response.HttpResponse;
import net.wxxr.http.util.Constants;
import net.wxxr.http.util.ErrorCodeUtil;

/**
 * 任务处理类
 * @author yuanwenfei
 * 2013-5-20下午10:52:51
 * @param <T>
 */
public class TaskCommond<T> {
	//需要执行的任务
	private Task<T> task;
	private HttpResponse<T> response;

	public void setResponse(HttpResponse<T> response) {
		this.response = response;
	}
	
	public void setTask(Task<T> task) {
		this.task = task;
	}

	public Task<T> getTask() {
		return task;
	}

	public HttpResponse<T> getResponse() {
		return response;
	}

	public boolean execute() {
		/**
		 * 包含结果码和结果数据
		 */
		HttpResult result = null;
		
		HttpBaseRequest request = task.getRequest();
		Object  o = request.getO();
		
		/**
		 * 请求模块标记
		 */
		ITag<T> tag = task.getTag();
		
		/**
		 * 设置Tag
		 */
		request.setITag(tag);
		
		
		/**
		 * 请求得到数据类型
		 */
		Class<T> clazz = task.getClazz();
		
		/**
		 * 数据解析结果码
		 */
		DataParseCode dataCode = null;
		
		/**
		 * 请求被取消
		 */
		if (request.isCancel()) {
			response.cancelled(tag);
			return false;
		}
		
		/**
		 * 在执行网络请求之前读缓存数据，读取成功返回缓存数据，否则，返回空值
		 */
		Object cacheData;
		if((cacheData = cacheBeforeExecute(request)) != null) {
			response.completed(tag, cacheData);
			return true;
		}
		
		/**
		 * 执行网络请求
		 */
		try {
			result = request.executeRequest();
		}catch (UnsupportedEncodingException e) {
			response.failed(tag, DataParseCode.ERROR_NET_ACCESS);
			return false;
		}
		
		/**
		 * 拷贝网络请求的返回的空结果数据
		 */
		if(copeNullResult(result, tag)) {
			response.failed(tag, DataParseCode.ERROR_NET_ACCESS);
			return false;
		}
		
		/**
		 * 取消请求
		 */
		if(result.getResultCode() == HttpCode.USER_CANCELLED) {
			response.cancelled(tag);
			return false;
		}
		
		/**
		 * 请求结果失败
		 */
		if(result.getResultCode() != HttpCode.STATUS_OK) {
			dataCode = ErrorCodeUtil.fromHttp2DataCode(result.getResultCode());
			response.failed(tag, dataCode);
			return false;
		}
		
		
		
		/**
		 * 请求成功并解析数据
		 */
		response.completed(tag, result, o, clazz);
		/**
		 * 获得数据对象
		 */
		cacheData = response.getParseData();
		/**
		 * 对数据对象进行缓存或者一些别的操作
		 */
		cacheAfterExecute(request, cacheData); 
		
		
		return true;
	}

	private boolean cookieLogonOut(HttpResult result){
		return ((result.getResultCode() == HttpCode.COOKIE_OUT) && (result.getResultCode() == HttpCode.FOUCE_LOGOUT)) ? true : false;
	}
	
	
	/**
	 * 在执行网络请求之前读缓存数据，读取成功返回缓存数据，否则，返回空值
	 * @param request
	 * @return
	 * @author yuanwenfei
	 */
	protected Object cacheBeforeExecute(HttpBaseRequest request) {
		return null;
	}
	
	/**
	 * 在数据处理后对其进行缓存
	 * @param request
	 * @param cacheData
	 * @author yuanwenfei
	 */
	protected void cacheAfterExecute(HttpBaseRequest request, Object cacheData) {
	}

	private boolean copeNullResult(HttpResult result, ITag<T> tag) {
		if(result == null) {
			DataParseCode dataCode = DataParseCode.ERROR_NET_ACCESS;
			response.failed(tag, dataCode);
			return true;
		}
		return false;
	}
	
}
